package com.sgb.kjwl.view.ui.activity.transport;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
;
import com.amap.api.maps.MapView;
import com.sgb.alimaplibrary.MapMarkerTools;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityGasMapBinding;
import com.sgb.kjwl.di.transport.DaggerTransportComponent;
import com.sgb.kjwl.di.transport.TransportModule;
import com.sgb.kjwl.model.entity.GasListInfoEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.transport.GoodsSourceLocationEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.widget.GoodsTypePopWindow3;
import com.sgb.kjwl.view.widget.MapGasPopWindow;
import com.sgb.kjwl.view.widget.SendGoodsLocationPopWindow3;
import com.sgb.kjwl.viewmodel.transport.MapGasModule;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 加油服务地图
 */
public class GasMapActivity extends AppActivity {
    ActivityGasMapBinding binding;
    @Inject
    MapGasModule module;
    private List<GoodsInfoTypeEntity3> brandList;

    private List<GoodsInfoTypeEntity3> typeList;

    // 一级联动数据
    public static List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> addressEntityList;
    // 二级联动数据
    public static List<List<AddressEntity<List<AddressEntity>>>> opt2 = new ArrayList<>();
    // 三级联动数据
    public static List<List<List<AddressEntity>>> opt3 = new ArrayList<>();
    private SendGoodsLocationPopWindow3 sendGoodsLocationPopWindow3;
    private GoodsTypePopWindow3 typePopWindow;
    private GoodsTypePopWindow3 brandPopWindow;
    private int position;
    private int position2;

    MapGasPopWindow mapGasPopWindow;
    private MapMarkerTools mapMarkerTools;
    private double currentLat = 0;
    private double currentLong = 0;
    List<GasListInfoEntity> bundles = new ArrayList<>();

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerTransportComponent.builder()
                .appComponent(appComponent)
                .transportModule(new TransportModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapMarkerTools = new MapMarkerTools(getActivity(), getLifecycle());
        initMap(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_gas_map);
        binding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //区域popWindow
        sendGoodsLocationPopWindow3 = new SendGoodsLocationPopWindow3(getActivity());
        sendGoodsLocationPopWindow3.setBgView(binding.grayLayout);
        sendGoodsLocationPopWindow3.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object proId, Object cityId, Object areaId) {
                GoodsSourceLocationEntity entity = (GoodsSourceLocationEntity) proId;
                binding.tvPlace.setText(" " + entity.getCityName() + "");
                module.infoEntity.setCityNo(entity.getCityNo());
                module.getFilter();


            }
        });
        binding.locationLayout.setOnClickListener(this);
        binding.gasBrancLayout.setOnClickListener(this);
        binding.gasTypeLayout.setOnClickListener(this);


        brandPopWindow = new GoodsTypePopWindow3(getActivity());
        brandPopWindow.setBgView(binding.grayLayout);
        brandPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object type, Object key2, Object key3) {
                GoodsInfoTypeEntity3 entity = (GoodsInfoTypeEntity3) type;
                List<String> brandList = new ArrayList<>();
                position = (int) key2;
                binding.tvBrand.setText(entity.getName());
                brandList.add(entity.getNo());
                module.infoEntity.setBrandNos(brandList);
                module.getFilter();
            }
        });
        typePopWindow = new GoodsTypePopWindow3(getActivity());
        typePopWindow.setBgView(binding.grayLayout);
        typePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object type, Object key2, Object key3) {
                GoodsInfoTypeEntity3 entity = (GoodsInfoTypeEntity3) type;

                position2 = (int) key2;
                binding.tvType.setText(entity.getName());

                module.infoEntity.setOilTypeNo(entity.getNo());
                module.getFilter();
            }
        });
    }

    @Override
    protected void init() {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.location_layout:
                setOptionStatus(2);
                if (null != addressEntityList && addressEntityList.size() > 0) {
                    showSendPopWindow();
                } else {
                    module.getAddressModel();
                }
                break;
            case R.id.gas_type_layout:
                if (typeList != null && typeList.size() > 0) {
                    showTypePop();

                } else {
                    module.oilType();
                }
                break;
            case R.id.gas_branc_layout:
                if (brandList != null && brandList.size() > 0) {
                    showBrandsPop();
                } else {
                    module.oilBrand();
                }
                break;
        }
    }

    //油品牌
    public void setBrandPop(List<GoodsInfoTypeEntity3> brandList) {
        this.brandList = brandList;
        showBrandsPop();

    }

    private void showBrandsPop() {
        brandPopWindow.showPopwindow(binding.line, 0, 0);
        brandPopWindow.setData(brandList, position);
    }

    //油类型
    public void setTypePop(List<GoodsInfoTypeEntity3> typeList) {
        this.typeList = typeList;
        // showTypePop();
        binding.tvType.setText(typeList.get(0).getName());

        module.infoEntity.setOilTypeNo(typeList.get(0).getNo());
        module.getFilter();
    }

    private void showTypePop() {
        typePopWindow.showPopwindow(binding.line, 0, 0);
        typePopWindow.setData(typeList, position2);
    }

    public void initAddressData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> datas) {
        addressEntityList = datas;
        initJsonData();
    }

    private void initJsonData() {
        for (int i = 0; i < addressEntityList.size(); i++) {
//            cityList = new ArrayList<>();
            ArrayList areaList = new ArrayList<>();
            opt2.add(addressEntityList.get(i).getChildren());
            for (int j = 0; j < addressEntityList.get(i).getChildren().size(); j++) {

                List<AddressEntity> cityAreaList = new ArrayList<>();
                List<AddressEntity> children = addressEntityList.get(i).getChildren().get(j).getChildren();
                if (children == null || children.size() == 0) {
                    cityAreaList.add(null);
                } else {
                    cityAreaList.addAll(children);
                }
                areaList.add(cityAreaList);
            }
            opt3.add(areaList);
        }
        showSendPopWindow();
    }

    private void showSendPopWindow() {
        sendGoodsLocationPopWindow3.showPopwindow(binding.line, 0, 0);
        sendGoodsLocationPopWindow3.setData(addressEntityList);
    }

    private void setOptionStatus(int index) {
        switch (index) {
            case 2:
                binding.tvPlace.setSelected(true);
                binding.purposeGoodsIv.setSelected(true);
                break;
            case 3:
                binding.tvType.setSelected(true);
                binding.typeIv.setSelected(true);
                break;
            case 4:
                binding.tvBrand.setSelected(true);
                binding.brandIv.setSelected(true);
                break;
        }
    }

    private void initMap(Bundle savedInstanceState) {
        mapGasPopWindow = new MapGasPopWindow(this);

        MapView mMapView = binding.mapView;
        mapMarkerTools.init(savedInstanceState, mMapView);
        mapMarkerTools.setMarkerClick(new MapMarkerTools.MarkerClick() {
            @Override
            public void onMarkerClick(int pos, String title) {
                showMarkerView(pos, title);
            }

            @Override
            public void onLocationChanged(Double Lat, Double Log, String address) {
                if (currentLat == 0 || currentLong == 0) {
                    currentLat = Lat;
                    currentLong = Log;
                    if (Log != 0 && Lat != 0) {
                        return;
                    }
                    module.infoEntity.setLatitude(Lat);
                    module.infoEntity.setLongitude(Log);
                    module.oilType();
                }
            }
        });
    }

    private void showMarkerView(int pos, String title) {
        GasListInfoEntity entity = bundles.get(pos);
        mapGasPopWindow.showPopwindowBottom(binding.mapView);
        mapGasPopWindow.setData(entity);
    }

    public void showMapList(List<GasListInfoEntity> list) {
        if (null != list && list.size() > 0) {
            currentLat = list.get(0).getLatitude();
            currentLong = list.get(0).getLongitude();
            cameraToPosition();
        }
        addMarkInfoWindow(list);
    }

    private void addMarkInfoWindow(List<GasListInfoEntity> datas) {
        try {
            mapMarkerTools.mapClear();
            bundles.clear();
            for (int i = 0; i < datas.size(); i++) {
                if (null != datas && datas.size() > 0) {
                    GasListInfoEntity entity = datas.get(i);
                    if (null != entity && 0 != entity.getLatitude() && 0 != entity.getLongitude()) {
                        double currentLat = entity.getLatitude();
                        double currentLong = entity.getLongitude();
                        if (currentLat == 0 || currentLong == 0) {
                            return;
                        }
                        mapMarkerTools.addPoiItem(currentLat, currentLong, "");
                        bundles.add(datas.get(i));
                    }
                }
            }
            mapMarkerTools.addAllMarkerToMap(R.mipmap.ic_map_overlay,false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //设置定位点
    private void cameraToPosition() {
        mapMarkerTools.setMyLocation(currentLat, currentLong);
    }
}
